package com.cytech.cloudclinic.utils;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import static android.view.View.DRAWING_CACHE_QUALITY_HIGH;


public class ImageUtils {
    /**
     * 传入宽高
     *
     * @param context
     * @param imageView
     * @param url
     * @param width
     * @param height
     */
    public static void show(Context context, ImageView imageView, String url, int width, int height) {
        url += String.format("?x-oss-process=image/resize,m_fixed,h_%d,w_%d", width, height);
        Glide.with(context).load(url).into(imageView);
    }

    /**
     * 宽高一样
     *
     * @param context
     * @param imageView
     * @param url
     * @param rect
     */
    public static void show(Context context, ImageView imageView, String url, int rect) {
        url += String.format("?x-oss-process=image/resize,m_fixed,h_%d,w_%d", rect, rect);
        Glide.with(context).load(url).into(imageView);
    }

    public static void show(Context context, ImageView imageView, String url) {
        Glide.with(context).load(url).into(imageView);
    }

    public static void showRound(Context context, ImageView imageView, int resourceId) {
        Glide.with(context)
                .load(resourceId)
                .apply(RequestOptions.circleCropTransform())
                .into(imageView);
    }

    public static void showRound(Context context, ImageView imageView, String url) {
        Glide.with(context)
                .load(url)
                .apply(RequestOptions.circleCropTransform())
                .into(imageView);
    }

    public Bitmap viewConversionBitmap(Context context, View v, int resourcesId) {
        int w = v.getWidth();
        int h = v.getHeight();
        Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565);
        Resources res = context.getResources();
        Bitmap bmp1 = BitmapFactory.decodeResource(res, resourcesId);
        /** 不设置canvas画布为白色，则生成透明 */
        //        c.drawColor(Color.WHITE);
        //以其中一张图片的大小作为画布的大小
        Bitmap bitmap = Bitmap.createBitmap(bmp.getWidth(), bmp
                .getHeight(), bmp1.getConfig());
        Canvas canvas = new Canvas(bitmap);
        //secondBitmap调整到和画布一样的大小
        Matrix m = new Matrix();
        //确定secondBitmap大小比例
        m.setScale(w / bmp1.getWidth(), h / bmp1.getHeight());
        Paint paint = new Paint();
        paint.setAlpha(150);
        canvas.drawBitmap(bmp1, 0, 0, null);
        canvas.drawBitmap(bmp, m, null);
        v.layout(0, 0, w, h);
        v.draw(canvas);
        return bitmap;
    }

    /**
     * view转bitmap
     */
    public static Bitmap createBitmapFromView(View view) {
        Bitmap bitmap = null;
        //开启view缓存bitmap
        view.setDrawingCacheEnabled(true);
        //设置view缓存Bitmap质量
        view.setDrawingCacheQuality(DRAWING_CACHE_QUALITY_HIGH);
        //获取缓存的bitmap
        Bitmap cache = view.getDrawingCache();
        if (cache != null && !cache.isRecycled()) {
            bitmap = Bitmap.createBitmap(cache);
        }
        //销毁view缓存bitmap
        view.destroyDrawingCache();
        //关闭view缓存bitmap
        view.setDrawingCacheEnabled(false);
        return bitmap;
    }

    /**
     * by mos on 2017.11.13
     * func:view转bitmap
     */
    public static Bitmap convertViewToBitmap(View view) {
        view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
        view.buildDrawingCache();
        Bitmap bitmap = view.getDrawingCache();
        return bitmap;
    }
}
